package com.huawei.hms.framework.network.restclient.hianalytics;

import android.content.Context;
import android.content.SharedPreferences.Editor;
import android.os.Process;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.common.PLSharedPreferences;
import com.huawei.hms.framework.common.StringUtils;
import com.huawei.hms.framework.common.hianalytics.HianalyticsHelper;
import com.huawei.hms.framework.network.restclient.Version;
import com.huawei.hms.framework.network.util.ContextUtil;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

public class CrashHandler implements UncaughtExceptionHandler {
    private static final String EVENT_ID = "networkkit_crash";
    private static final String EXCEPTION_NAME = "exception_name";
    private static final int INIT_CAPACITY = 1024;
    private static final String MESSAGE = "message";
    private static final String PREFS_NAME = "crash_analytics";
    private static final String PROCESS_ID = "process_id";
    private static final String SDK_NAME = "sdk_name";
    private static final String SDK_TYPE = "sdk_type";
    private static final String SDK_VERSION = "sdk_version";
    private static final String STACK_TRACE = "stack_trace";
    private static final String TAG = "CrashHandler";
    private static final String THREAD_ID = "thread_id";
    private static final String THREAD_NAME = "thread_name";
    private static final String TIME = "time";
    private Context context = ContextUtil.getContext();
    private UncaughtExceptionHandler oldExceptionHandler = null;

    public void init() {
        HianalyticsHelper.getInstance().getReportExecutor().execute(new Runnable() {
            public void run() {
                CrashHandler.this.oldExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
                Thread.setDefaultUncaughtExceptionHandler(CrashHandler.this);
                CrashHandler.this.reportLastCrashInfo();
            }
        });
    }

    private void reportLastCrashInfo() {
        if (HianalyticsHelper.getInstance().isEnableReportNoSeed(this.context)) {
            PLSharedPreferences pLSharedPreferences = new PLSharedPreferences(this.context, PREFS_NAME);
            if (pLSharedPreferences.getString(TIME, null) == null) {
                Logger.v(TAG, "not report");
                return;
            }
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            linkedHashMap.put("sdk_type", "UxPP");
            linkedHashMap.put("sdk_name", "Restclient_Crash");
            linkedHashMap.put("sdk_version", Version.getVersion());
            linkedHashMap.put(TIME, pLSharedPreferences.getString(TIME, ""));
            linkedHashMap.put(PROCESS_ID, pLSharedPreferences.getString(PROCESS_ID, ""));
            linkedHashMap.put(THREAD_ID, pLSharedPreferences.getString(THREAD_ID, ""));
            linkedHashMap.put(THREAD_NAME, pLSharedPreferences.getString(THREAD_NAME, ""));
            linkedHashMap.put("exception_name", pLSharedPreferences.getString("exception_name", ""));
            linkedHashMap.put("message", pLSharedPreferences.getString("message", ""));
            linkedHashMap.put(STACK_TRACE, pLSharedPreferences.getString(STACK_TRACE, ""));
            HianalyticsHelper.getInstance().onEvent(linkedHashMap, EVENT_ID);
            Logger.v(TAG, "%s", linkedHashMap);
            pLSharedPreferences.clear();
        }
    }

    public void uncaughtException(Thread thread, Throwable th) {
        if (HianalyticsHelper.getInstance().isEnableReportNoSeed(this.context)) {
            Logger.v(TAG, "crash coming");
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            linkedHashMap.put(TIME, "" + System.currentTimeMillis());
            linkedHashMap.put(PROCESS_ID, "" + Process.myPid());
            linkedHashMap.put(THREAD_ID, "" + Process.myTid());
            linkedHashMap.put(THREAD_NAME, thread.getName());
            linkedHashMap.put("exception_name", th.getClass().getName());
            linkedHashMap.put("message", StringUtils.anonymizeMessage(th.getMessage()));
            linkedHashMap.put(STACK_TRACE, getTraceInfo(th));
            saveData(linkedHashMap);
        }
        if (this.oldExceptionHandler != null) {
            this.oldExceptionHandler.uncaughtException(thread, th);
        }
    }

    private void saveData(LinkedHashMap<String, String> linkedHashMap) {
        if (linkedHashMap != null) {
            Logger.v(TAG, "data = %s", linkedHashMap);
            try {
                PLSharedPreferences pLSharedPreferences = new PLSharedPreferences(this.context, PREFS_NAME);
                Editor edit = pLSharedPreferences.edit();
                for (Entry entry : linkedHashMap.entrySet()) {
                    edit.putString((String) entry.getKey(), (String) entry.getValue());
                }
                edit.apply();
                Logger.v(TAG, pLSharedPreferences.getString("message", null));
            } catch (InternalError e) {
            }
        }
    }

    private String getTraceInfo(Throwable th) {
        StackTraceElement[] stackTrace = th.getStackTrace();
        StringBuilder stringBuilder = new StringBuilder(1024);
        for (StackTraceElement stackTraceElement : stackTrace) {
            stringBuilder.append("at ").append(stackTraceElement.toString()).append(";");
        }
        return stringBuilder.toString();
    }
}
